package dw2;
import java.awt.*;
import java.util.Random;

public class Bullet
{
    private Rectangle bounds;
    private Random random = new Random();
    private int x = 0, y = 0;
    private double direction = 0, speed = 0;
    
    private int radius = 0;
    private Color color;
    private int damage;
    private double gravity = .01;


    public Bullet(Main parent,int pos_x, int pos_y, Color color, int radius,int damage)
    {
        bounds = new Rectangle(x,y,radius,radius);
        this.radius = radius;
        x = pos_x;
        y = pos_y;
        this.color = color;
        this.damage = damage;
    }



    public Rectangle getBounds()
    {
        return bounds;
    }
    
    public void setXYspeed(double x, double y)
    {
        double H = Math.sqrt((x)*(x) + (y)*(y)); // The hypotenuse
        double angle = Math.acos(x/H);
        angle = angle * 57.2960285258;
        if(y < 0)
        {
            angle = 360-angle;
        }
        direction = angle;
        speed = Worm.getSpeed(x, y);
    }

    public void action(Main mainparent,Worm player)
    {

        move(mainparent.getSize());

        if(player.hitWorm(bounds)==true)
        {

            player.hurtWorm(damage);
    
            mainparent.removeBullet(this);
            return;
        }
        if(y > mainparent.getHeight()/2)
        {
            mainparent.removeBullet(this);
            return;
        }

    }
    public void move(Dimension bounds)
    {
     	setXYspeed(Worm.getXspeed(direction, speed), Worm.getYspeed(direction, speed) + gravity);


        x += Worm.getXspeed(direction, speed);
        y += Worm.getYspeed(direction, speed);
    }

    public void draw(Graphics g,Main mainparent)
    {
        g.setColor(color);
        int move = mainparent.getSize().width/2;
        move -= mainparent.getScroll();
        bounds.setLocation(move+(x-radius),(y-radius));
        g.fillOval(move+(x-radius),(y-radius),(radius),(radius));
    }

    public int getX(){
        return x;
    }
}